package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb08Xzqh;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb08XzqhExc;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.DirTree;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb08XzqhService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 行政区划
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "行政区划")
@RestController
@RequestMapping("/dictionary/cb08Xzqh")
@Validated
public class Cb08XzqhController {

    @Resource
    private ICb08XzqhService iCb08XzqhService;

    /**
     * 添加行政区划
     */
    @ApiOperation("添加行政区划")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb08Xzqh cb08Xzqh) {
        iCb08XzqhService.save(cb08Xzqh);
        return CommonResult.ok();
    }

    /**
     * 删除行政区划
     */
    @ApiOperation("删除行政区划")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb08XzqhService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑行政区划
     */
    @ApiOperation("编辑行政区划")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb08Xzqh cb08Xzqh) {
        iCb08XzqhService.updateById(cb08Xzqh);
        return CommonResult.ok();
    }

    /**
     * 行政区划分页查询
     */
    @ApiOperation("行政区划分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb08Xzqh>> page() {
        return CommonResult.data(iCb08XzqhService.page(new Cb08Xzqh()));
    }
    /**
     * 行政区划目录树
     */
    @ApiOperation("行政区划目录树")
    @GetMapping("/xzqhDirTree")
    public CommonResult<DirTree> getXzqhDirTree(
           Integer fdm
    ) {
//        return CommonResult.data(iCb08XzqhService.getXzqhDirTree(fdm));
        //重构
        return CommonResult.data(iCb08XzqhService.getXzqhTree(fdm));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb08XzqhExc> list = ExcelImportUtil.readMultipartFile(file, Cb08XzqhExc.class);
            List<Cb08Xzqh> cb01Zhs = GsonUtil.ObjectToList(list, Cb08Xzqh.class);
            iCb08XzqhService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
